<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Login extends MX_Controller {

	public function __construct() {
		parent::__construct();
		
		$this->template->set_theme('admin');
		$this->template->set_layout('login');	
		
		
		
	}
	
	public function index(){
		
		$this->template->build('login/login');
		
	}
	
	public function submit()
	{
		//if($this->input->post('login'))
		//{
			//print "Subh";
			//die();
			$this->session->unset_userdata('message');
			$this->form_validation->set_rules('admin_user', 'admin_user', 'required|xss_clean');
			$this->form_validation->set_rules('admin_password', 'admin_password', 'required|xss_clean');
				
			if($this->form_validation->run())
			{
				$this->db->where('email', $this->input->post('admin_user',TRUE));
				$this->db->where('password', $this->input->post('admin_password',TRUE));
				$this->db->where('status', '51');
				$query = $this->db->get('member');
	
				if ($query->num_rows() == 1)
				{
					$row = $query->row();
					$data = array
					(
							'last_login_date' => date('m/d/Y h:i:s a', time()),
							'last_login_ip' => $this->input->ip_address()
					);
					//--Update last_login n IP09
					$this->db->where('email',$this->input->post('admin_user',TRUE));
					$this->db->update('member',$data);
						
					// Our user exists, set session.
					$this->session->set_userdata('sess_admin_id'			, $row->id);
					$this->session->set_userdata('sess_admin_user'			, $row->email);
					$this->session->set_userdata('sess_admin_password'		, $row->password);
					$this->session->set_userdata('sess_admin_name'			, $row->name);
					$this->session->set_userdata('sess_admin_last_login'		, $row->last_login_date);
					$this->session->set_userdata('sess_admin_last_ip'		, $row->last_login_ip);
					$this->session->set_userdata('sess_admin_status'			, $row->status);
					$this->session->set_userdata('sess_admin_image'			, $row->image);
						
						
					redirect ('secure/dashboard');
				}
				else
				{
					$this->session->set_flashdata('message','Error! Invalid Login..');
					$this->index();
				}
			}
			else
			{
				$this->session->set_userdata('message','Error! Invalid Login..');
				$this->index();
			}
		//}
	}
	function logout() {
		
		
		//print "HELLO";
		//die();
		
		$this->session->unset_userdata('sess_admin_user');
		$this->session->unset_userdata('sess_admin_password');
		$this->session->unset_userdata('sess_admin_name');
		$this->session->unset_userdata('sess_admin_last_login');
		$this->session->unset_userdata('sess_admin_last_ip');
		$this->session->unset_userdata('sess_admin_status');
		redirect('secure/dashboard');
	}
	
	
}	
/* End of file*/	